package com.musemun.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.zxing.NotFoundException;
import com.musemun.exception.MyDefineException;
import com.musemun.pojo.Permission;
import com.musemun.pojo.RolePermission;
import com.musemun.service.PermissionService;
import com.musemun.service.RolePermissionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
/**
 * <p>
 *     继承FilterInvocationSecurityMetadataSource 设置为从数据库中查询所有角色并找到的对应的角色返回给MyAccessDecisionManager：权限判断决策器
 * </p>
 * @author winner
 * @date 2020/5/9
 */
@Component
public class MyInvocationSecurityMetadataSource implements FilterInvocationSecurityMetadataSource {
    Logger log= LoggerFactory.getLogger(MyInvocationSecurityMetadataSource.class);
    @Resource
    private RolePermissionService rolePermissionService;
    private static HashMap<String,Collection<ConfigAttribute>> map=null;
    /**
     *
     *
     * @param object 可以看作是传入的url类
     * @return 返回值url中的访问的是哪个路径
     */
    @Override
    public Collection<ConfigAttribute> getAttributes(Object object) throws IllegalArgumentException {
        if(map == null){
            loadResourceDefine();
        }
        HttpServletRequest httpRequest = ((FilterInvocation) object).getHttpRequest();
        for (String url: map.keySet()) {
            if(new AntPathRequestMatcher(url).matches(httpRequest))
            {
                log.info("--------------url="+url,"----------------");
                return map.get(url);
            }
        }
       return null;
    }
    /**
     *  获得角色对应的权限
     *                  key  value
     *   map格式        url: List(roleA,roleB,roleC)
     *
     * @return map
     */

    private void loadResourceDefine() {
        map=new HashMap<>(64);
        List<RolePermission> rolePermissions = rolePermissionService.getAllRolePermission();
        rolePermissions.forEach(rolePermission->{
            String url = rolePermission.getUrl();
            String name=rolePermission.getRole();
            ConfigAttribute role =new SecurityConfig(name);
            if(!map.containsKey(url)){
                List<ConfigAttribute>list =new ArrayList<>();
                list.add(role);
                map.put(url,list);
            }else{
                map.get(url).add(role);
            }
        });
    }

    @Override
    public Collection<ConfigAttribute> getAllConfigAttributes() {
        return null;
    }

    @Override
    public boolean supports(Class<?> clazz) {
        return true;
    }
}
